package com.sunstar.club.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sunstar.club.subject.application.convert.SubjectAnswerDtoConverter;
import com.sunstar.club.subject.application.convert.SubjectInfoDtoConverter;
import com.sunstar.club.subject.application.dto.SubjectInfoDto;
import com.sunstar.club.subject.common.result.PageResult;
import com.sunstar.club.subject.common.result.Result;
import com.sunstar.club.subject.domain.entity.SubjectAnswerBo;
import com.sunstar.club.subject.domain.entity.SubjectInfoBo;
import com.sunstar.club.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目Controller
 *
 * @Author: Shylo Sun
 * @Project: track-club
 * @Date: 2024/2/14 17:13
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     *
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDto));
            }

            // 参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDto.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getLabelIds()), "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToInfoBo(subjectInfoDto);
            List<SubjectAnswerBo> subjectAnswerBos = SubjectAnswerDtoConverter.INSTANCE.converListDtoToBo(subjectInfoDto.getOptionList());
            subjectInfoBo.setOptionList(subjectAnswerBos);
            subjectInfoDomainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    /**
     * 查询题目列表
     *
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDto));
            }

            // 参数校验
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(), "标签id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToInfoBo(subjectInfoDto);
            PageResult<SubjectInfoBo> boPageResult = subjectInfoDomainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目列表失败");
        }
    }

    /**
     * 查询题目详情
     *
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDto));
            }

            // 参数校验
            Preconditions.checkNotNull(subjectInfoDto.getId(), "题目id不能为空");

            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.convertDtoToInfoBo(subjectInfoDto);
            SubjectInfoBo boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto dtoResult = SubjectInfoDtoConverter.INSTANCE.convertBoToInfoDto(boResult);
            return Result.ok(dtoResult);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

}
